package com.admin.service;

import com.data.entity.ServerMassage;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Properties;

@Service
public class AdminIndexService {

    public ServerMassage getServerMassage(HttpServletRequest request) {
        ServerMassage serverMassage = new ServerMassage();
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        //获取本机ip
        String ip = addr.getHostAddress();
        //获取本机计算机名称
        String hostName = addr.getHostName();
        serverMassage.setIp(ip);
        serverMassage.setHostname(hostName);
        Properties pro = System.getProperties();
        //主机名称
        serverMassage.setHostname(pro.getProperty("user.name"));
        //操作系统java.version
        serverMassage.setSystem(pro.getProperty("os.name"));
        //jdk版本
        serverMassage.setJdk(pro.getProperty("java.version"));
        //服务器端口
        serverMassage.setPort(request.getServerPort());
        //项目路径
        serverMassage.setProjectUrl(request.getContextPath());
        //request的id
        serverMassage.setSessionId(request.getSession().getId());
        int count = (int) request.getServletContext().getAttribute("count");
        //在线人数
        serverMassage.setSession(count);
        return serverMassage;
    }
}
